package me.xplabs.model 
{
	import me.xplabs.battle.buff.Buff;
	import me.xplabs.Globals;
	import me.xplabs.interfaces.IAstarFindPath;
	import me.xplabs.model.combatunit.BaseBuff;
	import me.xplabs.model.combatunit.BaseRole;
	import me.xplabs.model.combatunit.MRoleInfo;
	import me.xplabs.model.energy.EnergyData;
	import me.xplabs.model.msg.MsgHitActor;
	import me.xplabs.msg.NotificationFight;
	import me.xplabs.msg.NotificationName;
	import me.xplabs.msg.NotificationRaidInfo;
	import me.xplabs.utils.SimpleObjPool;
	/**
	 * ...
	 * @author xiaohan
	 */
	public class RoleDataManager 
	{
		private var _rolePool:SimpleObjPool;
		private var _roles:Vector.<MRoleInfo>;
		private var _prevTime:uint;
		public function RoleDataManager() 
		{
			_roles = new Vector.<MRoleInfo>();
			_rolePool = new SimpleObjPool(MRoleInfo, 1, 1);
		}
		
		public function update():void
		{
			var tempGapTime:uint = Globals.timeManager.now - _prevTime;
			_prevTime = Globals.timeManager.now;
			//print(_prevTime, Globals.timeManager.now, _prevTime - Globals.timeManager.now);
			var len:int = _roles.length;
			for (var i:int = len - 1; i >= 0 ; i--)
			{
				_roles[i].heartbeat(tempGapTime);
				if (_roles[i].move())
				{
					//print("敌人进入我方阵营！！" + i);
					var loseLife:int = _roles[i].loseLife;
					removeActor(_roles.splice(i, 1)[0]);
					sendMsg(NotificationName.UPDATE_RAIDINFO, -loseLife, NotificationRaidInfo.UPDATE_LIFE);
				}
			}
		}
		public function get hasRole():Boolean
		{
			return _roles.length == 0;
		}
		/**
		 * 添加一个行动者到地图中
		 * @param	p_actor
		 */
		public function addActor(p_base:EnergyData, p_role:BaseRole,aStar:IAstarFindPath):void
		{
			var tempActor:MRoleInfo = _rolePool.getObj();
			//var tempActor:MRoleInfo = new MRoleInfo();
			tempActor.soleId  = Globals.soleId;
			tempActor.setStartPos(p_base.startPos.x, p_base.startPos.y);
			tempActor.setEndPos(p_base.endPos.x, p_base.endPos.y);
			tempActor.id = p_role.id;
			tempActor.title = p_role.title;
			tempActor.resId = p_role.resId;
			tempActor.attack = p_role.attack;
			tempActor.attackSpeed = p_role.attackSpeed;
			tempActor.speed = p_role.speed;
			tempActor.life = p_role.life;
			tempActor.curLife = p_role.life;
			tempActor.attackArea = p_role.attackArea;
			tempActor.describe = p_role.describe;
			tempActor.loseLife = p_role.loseLife;
			tempActor.gainGold = p_role.gainGold;
			tempActor.cellPos = changeCellPosHandler;
			tempActor.paths = aStar.find(tempActor.startPos.x, tempActor.startPos.y, tempActor.endPos.x, tempActor.endPos.y);
			_roles.push(tempActor);
			sendMsg(NotificationName.FIGHT_NOTIFICATION, tempActor, NotificationFight.APPEAR_ENERGY);
		}
		
		private function changeCellPosHandler(p_actor:MRoleInfo):void 
		{
			if (!p_actor) return;
			RolePositionManager.i.setCellActor(p_actor);
			//_mMapData.setCellActor(p_actor);
		}
			/**
		 * 删除一个行动者
		 * @param	p_id
		 */
		public function delActor(p_soleId:int):void
		{
			var len:int = _roles.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_roles[i].soleId == p_soleId)
				{
					removeActor(_roles.splice(i, 1)[0]);
					break;
				}
			}
		}
		/**
		 * 角色收到伤害的消息
		 * @param	msgHitActor
		 */
		public function hitActor(msgHitActor:MsgHitActor):void 
		{
			var len:int = _roles.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_roles[i].soleId == msgHitActor.actorId)
				{
					_roles[i].curLife = Math.max(0, _roles[i].curLife-msgHitActor.hitValue);
					_roles[i].byHit++;
					//trace(_roles[i].byHit);
					if (_roles[i].curLife <= 0)
					{
						sendMsg(NotificationName.UPDATE_RAIDINFO, +_roles[i].gainGold, NotificationRaidInfo.UPDATE_GOLD);
						removeActor(_roles.splice(i, 1)[0]);
					}
					return;
				}
			}
			//var paths:*= RolePositionManager.i.paths;
			//trace("又能");
		}
		
		
		public function addBuff(p_roleId:int, baseBuff:BaseBuff):void
		{
			var len:int = _roles.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_roles[i].soleId == p_roleId)
				{
					//var buff:Buff = MacroObjPool.getObj(Buff, 1, 1);
					var buff:Buff = new Buff();
					buffAssignment(buff, baseBuff);
					buff.soleId = Globals.soleId;
					_roles[i].addBuff(buff);
				}
			}
		}
		public function delBuff(roleId:int, baseId:int):void
		{
			var len:int = _roles.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_roles[i].soleId == roleId)
				{
					_roles[i].removeBuffByBaseId(baseId);
					break;
				}
			}
		}
		private function buffAssignment(buff:Buff, baseBuff:BaseBuff):void
		{
			buff.baseId 	= baseBuff.baseId;
			buff.id 		= baseBuff.id;
			buff.icon 		= baseBuff.icon;
			buff.describe 	= baseBuff.describe;
			buff.type 		= baseBuff.type;
			buff.effect 	= baseBuff.effect;
			buff.time 		= baseBuff.time;
			buff.gap	 	= baseBuff.gap;
			buff.magic 		= baseBuff.magic;
			buff.level 		= baseBuff.level;
		}
		
		
		
		
		
		public function dispose():void
		{
			var len:int = _roles.length;
			for (var i:int = 0; i < len; i++) 
			{
				removeActor(_roles[i]);
			}
			_roles.length = 0;
		}
		
		
		
		private function removeActor(p_actor:MRoleInfo):void
		{
			sendMsg(NotificationName.FIGHT_NOTIFICATION, p_actor.soleId, NotificationFight.DISAPPEAR_ENERGY);
			RolePositionManager.i.deleteCellActor(p_actor.prevCellPos,p_actor.curCellPos, p_actor.soleId);
			Globals.delSoleId(p_actor.soleId);
			p_actor.dispose();
			_rolePool.dispose(p_actor);
			p_actor = null;
			//Utils.print("敌人消失啦");
		}
	}

}